package com.qf.fronted.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;


// 单纯的做一件事的服务，没必要写接口。
public class SmsService {

    public void sendSms(String code, String phone) {
        // TODO 调用第三方的短信接口。 http://aliyun.com/sms/sendMsg
        String accessKeyId = "LTAI4FzwGB3YAFd1r4M4Pf3P";
        String accessSecret = "92mKpSBAcXNEpQKho3MhLe8blmQedh";
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        //json格式  "{\"code\":123456}"
        String codeStr = "{\"code\":"+code+"}";
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "叮咚123影院");
        request.putQueryParameter("TemplateCode", "SMS_203185867");
        request.putQueryParameter("TemplateParam", codeStr);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println("往手机号为：" + phone + ", 发送验证码为：" + code);
    }
}
